﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using WebSocketSharp;

namespace WebsocketTest
{
    internal class WeSocketClient : AISocket
    {
        /// <summary>
        /// 获得情感分析
        /// </summary>
        /// <returns>true 好的情感，false 不好的情感</returns>
        public bool GetSentimentClassify()
        {
            Console.WriteLine("Websocket Demo");
            WebSocket web = new WebSocket("ws://localhost:16666");
            web.OnMessage += Web_OnMessage;

            web.Connect();
            Student_info student_demo = new Student_info();
            student_demo.studentName = "小毛";
            student_demo.id = 181000000;
            student_demo.score = 91;

            // web.Send(JsonSerializer.Serialize(student_demo));
            web.Send(JsonSerializer.SerializeToUtf8Bytes(student_demo));
            Console.ReadKey();


            // 情感分析+回复
            MsgInfo msgInfo = new MsgInfo();
            msgInfo.MsgEvent = "strEmotionClassify";
            msgInfo.MsgText = "你这个毒妇，朕要杀了你!";
            web.Send(JsonSerializer.SerializeToUtf8Bytes(msgInfo));

            Console.ReadKey();
            web.Close();
            //Console.ReadLine();

            return true;
        }

        private static void Web_OnMessage(object? sender, MessageEventArgs e)
        {
            Console.WriteLine("Received a message of " + e.ToString());

            // 在python中转unicode编码，我对c#不是很熟悉
            Console.WriteLine("++ Received a message of " + e.Data);

        }
    }

    class Student_info
    {
        public string studentName { get; set; }
        public int id { get; set; }
        public int score { get; set; }
    }

    class MsgInfo
    {
        private string msgEvent;
        private string msgText;
        public string MsgEvent { get; set; }
        public string MsgText { get; set; }
    }

}
